//
//  KongWSearchViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "KongWSearchViewController.h"
#import "KongWURLSessionHandle.h"
#import "KongWFindDoctorModel.h"
#import "KongWFindDoctorTableViewCell.h"
#import "KongWFindDoctorDetailViewController.h"
#import "KongWFindHospitanModel.h"
#import "KongWfindHospitalTableViewCell.h"
#import "KongWFindHospitalDetailViewController.h"
#import "KongWIllnessDetailModel.h"
#import "KongWIllnessDetailTableViewCell.h"
#import "KongWCellSelectDetailViewController.h"
#import <SVProgressHUD.h>
#import <MJRefresh.h>
#define kHeight     [UIScreen mainScreen].bounds.size.height / 667
#define kWidth      [UIScreen mainScreen].bounds.size.width / 375
@interface KongWSearchViewController ()<UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate>

@property (retain, nonatomic) UITextField * textField;

@property (retain, nonatomic) UIView *chooseView;

@property (retain, nonatomic) UIButton *searchLeftBtn;

@property (retain, nonatomic) UIButton *doctorBtn;

@property (retain, nonatomic) UIButton *hospitalBtn;

@property (retain, nonatomic) UIButton *illnessBtn;

@property (retain, nonatomic) UILabel *line1;

@property (retain, nonatomic) UILabel *line2;

@property (assign, nonatomic) NSInteger type;

@property (retain, nonatomic) UITableView *table;

@property (retain, nonatomic) UIView *backGroundView;

@property (retain, nonatomic) NSMutableArray *doctorArray;

@property (retain, nonatomic) NSMutableArray *hospitalArray;

@property (retain, nonatomic) NSMutableArray *illnessArray;

@property (assign, nonatomic) NSInteger page;

@property (retain, nonatomic) UILabel *headerLabel;

@end

@implementation KongWSearchViewController
-(void)dealloc
{
    [_textField release];
    [_chooseView release];
    [_searchLeftBtn release];
    [_doctorBtn release];
    [_hospitalBtn release];
    [_illnessBtn release];
    [_line1 release];
    [_line2 release];
    [_keyWord release];
    [_table release];
    [_backGroundView release];
    [_doctorArray release];
    [_hospitalArray release];
    [_illnessArray release];
    [_headerLabel release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.doctorArray = [NSMutableArray array];
    self.hospitalArray = [NSMutableArray array];
    self.illnessArray = [NSMutableArray array];
    self.type = 3;
    self.page = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 30);
    self.title = @"搜索";
    [backBtn setBackgroundImage:[UIImage imageNamed:@"KongWback"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self loadData];
     [self creatSearchView];
    [self creatTableView];
   
}

-(void)creatSearchView
{
    self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(kWidth * 10, kHeight * 74, self.view.frame.size.width - kWidth * 60, kHeight * 40)];
    self.backGroundView.layer.cornerRadius = 20;
    self.backGroundView.layer.borderWidth = 2;
    self.backGroundView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:self.backGroundView];
    [self.backGroundView release];
    
    self.searchLeftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.searchLeftBtn.frame = CGRectMake(kWidth * 10, 0, kWidth * 80, kHeight * 40);
    [self.searchLeftBtn addTarget:self action:@selector(searchLeftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchLeftBtn setTitle:@"查疾病" forState:UIControlStateNormal];
    
    [self.searchLeftBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.backGroundView addSubview:self.searchLeftBtn];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(self.searchLeftBtn.frame.origin.x + self.searchLeftBtn.frame.size.width + kWidth * 5, 0, 0.5, self.searchLeftBtn.frame.size.height)];
    line.backgroundColor = [UIColor grayColor];
    [self.backGroundView addSubview:line];
    [line release];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(line.frame.origin.x + kWidth * 6, 0, self.backGroundView.frame.size.width - line.frame.origin.x - kWidth * 6, kHeight * 40)];
    self.textField.textAlignment = NSTextAlignmentLeft;
    [self.textField setBorderStyle:UITextBorderStyleNone];
    self.textField.returnKeyType = UIReturnKeyDefault;
    self.textField.clearButtonMode = UITextFieldViewModeAlways;
    self.textField.delegate = self;
    self.textField.placeholder = @"请输入搜索关键字";
    self.textField.text = self.keyWord;
    [self.backGroundView addSubview:self.textField];
    [self.textField release];
    
    UIButton *searchRightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchRightBtn.frame = CGRectMake(self.backGroundView.frame.origin.x + self.backGroundView.frame.size.width + kWidth * 10, self.backGroundView.frame.origin.y + kHeight * 5, kWidth * 30, kWidth * 30);
    [searchRightBtn setBackgroundImage:[UIImage imageNamed:@"KongWsearchIcon"] forState:UIControlStateNormal];
    [searchRightBtn addTarget:self action:@selector(searchRightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:searchRightBtn];
    [self creatChooseView];
    
}

-(void)creatChooseView
{
    self.chooseView = [[UIView alloc] init];
    self.chooseView.layer.borderWidth = 1;
    self.chooseView.layer.borderColor = [UIColor grayColor].CGColor;
    self.chooseView.backgroundColor = [UIColor whiteColor];
    self.chooseView.frame = CGRectMake(kWidth * 20, kHeight * 114, self.searchLeftBtn.frame.size.width, 0);
    [self.view addSubview:self.chooseView];
    [self.chooseView release];
}

-(void)loadData
{
    [KongWURLSessionHandle handleGETWithUrlString:[NSString stringWithFormat:@"http://api.5ichong.com/v1.2/search/find?longitude=4.9E-324&latitude=4.9E-324&page=%ld&limit=20&type=%ld&keyword=%@", self.page, self.type, self.textField.text] parameters:nil showHuD:YES onView:self.view successfulBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        
        if ((int)[dic objectForKey:@"status"] != 10003) {
        
            for (NSDictionary *dictionary in [[dic objectForKey:@"data"] objectForKey:@"list"]) {
            
                if (self.type == 1) {
                    KongWFindHospitanModel *hospital = [KongWFindHospitanModel findHospitalWithDictionary:dictionary];
                    [self.hospitalArray addObject:hospital];
                }else if (self.type == 2)
                {
                    KongWFindDoctorModel *doctor = [KongWFindDoctorModel findDoctorWithDictionary:dictionary];
                    [self.doctorArray addObject:doctor];
                }else
                {
                    KongWIllnessDetailModel *illness = [KongWIllnessDetailModel illnessDetailWithDictionary:dictionary];
                    [self.illnessArray addObject:illness];
                }
            }
        }
        [self.table reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"Error:%@", error);
    }];
}

-(void)creatHeadLabel
{
    
    self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kHeight * 30)];
    if (self.type == 1) {
        self.headerLabel.text = [NSString stringWithFormat:@"关于%@的医院搜索", self.textField.text];
    }else if (self.type == 2)
    {
        self.headerLabel.text = [NSString stringWithFormat:@"关于%@的医生搜索", self.textField.text];
    }else
    {
        self.headerLabel.text = [NSString stringWithFormat:@"关于%@的疾病搜索", self.textField.text];
    }
    
    self.headerLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
    self.headerLabel.textColor = [UIColor colorWithRed:0.0 green:0.454 blue:1.0 alpha:1.0];
}


-(void)creatTableView
{
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, kHeight * 114, self.view.frame.size.width, self.view.frame.size.height - self.backGroundView.frame.origin.y - self.backGroundView.frame.size.height) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.tableHeaderView = self.headerLabel;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (self.type == 2) {
        [self.table registerClass:[KongWFindDoctorTableViewCell class] forCellReuseIdentifier:@"searchDoctor"];
    }else if (self.type == 1)
    {
        [self.table registerClass:[KongWfindHospitalTableViewCell class] forCellReuseIdentifier:@"searchHospital"];
    }else
    {
        [self.table registerClass:[KongWIllnessDetailTableViewCell class] forCellReuseIdentifier:@"searchIllness"];
    }
    [self.view addSubview:self.table];
    [self.table release];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.type == 1) {
        return self.hospitalArray.count;
    }else if (self.type == 2)
    {
        return self.doctorArray.count;
    }else
    {
        return self.illnessArray.count;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == 1) {
        return 110;
    }else if (self.type == 2)
    {
        return 170;
    }else
    {
        return 100;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == 1) {
        KongWfindHospitalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchHospital"];
        cell.findHospital = [self.hospitalArray objectAtIndex:indexPath.row];
        return cell;
    }else if (self.type == 2)
    {
        KongWFindDoctorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchDoctor"];
        cell.findDoctorModel = [self.doctorArray objectAtIndex:indexPath.row];
        return cell;
    }else
    {
        KongWIllnessDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchIllness"];
        cell.illnessDetailModel = [self.illnessArray objectAtIndex:indexPath.row];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == 1) {
        KongWFindHospitalDetailViewController *findHospitalDetail = [[KongWFindHospitalDetailViewController alloc] init];
        findHospitalDetail.udid = ((KongWFindHospitanModel *)[self.hospitalArray objectAtIndex:indexPath.row]).ID;
        [findHospitalDetail setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:findHospitalDetail animated:YES];
        [findHospitalDetail release];
        
        
    }else if (self.type == 2)
    {
        KongWFindDoctorDetailViewController *findDoctorDetail = [[KongWFindDoctorDetailViewController alloc] init];
        findDoctorDetail.udid = ((KongWFindDoctorModel *)[self.doctorArray objectAtIndex:indexPath.row]).uid;
        [findDoctorDetail setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:findDoctorDetail animated:YES];
        [findDoctorDetail release];
        
    }else
    {
        KongWCellSelectDetailViewController *cellSelectDetail = [[KongWCellSelectDetailViewController alloc] init];
        cellSelectDetail.uid = ((KongWIllnessDetailModel *)[self.illnessArray objectAtIndex:indexPath.row]).ID;
        [self.navigationController pushViewController:cellSelectDetail animated:YES];
        [cellSelectDetail release];
    }

}
-(void)doctorBtnAction:(id)sender
{
    
    [self.searchLeftBtn setTitle:@"找医生" forState:UIControlStateNormal];
    [self backView];
    self.searchLeftBtn.selected = NO;
    self.type = 2;
    [self.table removeFromSuperview];
    [self creatTableView];
    [self.chooseView removeFromSuperview];
    [self creatChooseView];
   
    
}

-(void)hospitalBtnAction:(id)sender
{
    
    [self.searchLeftBtn setTitle:@"找医院" forState:UIControlStateNormal];
    [self backView];
    self.searchLeftBtn.selected = NO;
    self.type = 1;
    [self.table removeFromSuperview];
    [self creatTableView];
    [self.chooseView removeFromSuperview];
    [self creatChooseView];
    
    
}

-(void)illnessBtnAction:(id)sender
{
    
    [self.searchLeftBtn setTitle:@"查疾病" forState:UIControlStateNormal];
    [self backView];
    self.searchLeftBtn.selected = NO;
    self.type = 3;
    [self.table removeFromSuperview];
    [self creatTableView];
    [self.chooseView removeFromSuperview];
    [self creatChooseView];
   
    
}

-(void)searchLeftBtnAction:(UIButton *)sender
{
    
    sender.selected = !sender.isSelected;
    if (sender.isSelected) {
        [self pushView];
        [self.view bringSubviewToFront:self.chooseView];
    }else
    {
        [self backView];
    }
    
}
-(void)searchRightBtnAction:(id)sender
{
    
    [self loadData];
    [self.textField resignFirstResponder];
    
}
#pragma mark search动画操作
-(void)pushView
{
    [UIView animateWithDuration:0.1 animations:^{
        self.chooseView.frame = CGRectMake(kWidth * 20, kHeight * 114, self.searchLeftBtn.frame.size.width, self.searchLeftBtn.frame.size.height * 3);
        
        self.doctorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.doctorBtn.frame = CGRectMake(0, 0, self.chooseView.frame.size.width, self.chooseView.frame.size.height / 3);
        [self.doctorBtn setTitle:@"找医生" forState:UIControlStateNormal];
        [self.doctorBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.doctorBtn addTarget:self action:@selector(doctorBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.chooseView addSubview:self.doctorBtn];
        
        self.line1 = [[UILabel alloc] initWithFrame:CGRectMake(self.doctorBtn.frame.origin.x, self.doctorBtn.frame.origin.y + self.doctorBtn.frame.size.height, self.doctorBtn.frame.size.width, 1)];
        self.line1.backgroundColor = [UIColor grayColor];
        [self.chooseView addSubview:self.line1];
        
        self.hospitalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.hospitalBtn.frame = CGRectMake(0, self.chooseView.frame.size.height / 3, self.chooseView.frame.size.width, self.chooseView.frame.size.height / 3);
        [self.hospitalBtn setTitle:@"找医院" forState:UIControlStateNormal];
        [self.hospitalBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.hospitalBtn addTarget:self action:@selector(hospitalBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.chooseView addSubview:self.hospitalBtn];
        
        self.line2 = [[UILabel alloc] initWithFrame:CGRectMake(self.hospitalBtn.frame.origin.x, self.hospitalBtn.frame.origin.y + self.hospitalBtn.frame.size.height, self.hospitalBtn.frame.size.width, 1)];
        self.line2.backgroundColor = [UIColor grayColor];
        [self.chooseView addSubview:self.line2];
        
        self.illnessBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.illnessBtn.frame = CGRectMake(0, self.chooseView.frame.size.height / 3 * 2, self.chooseView.frame.size.width, self.chooseView.frame.size.height / 3);
        [self.illnessBtn setTitle:@"查疾病" forState:UIControlStateNormal];
        [self.illnessBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.illnessBtn addTarget:self action:@selector(illnessBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.chooseView addSubview:self.illnessBtn];
    }];
}

-(void)backView
{
    [UIView animateWithDuration:0.1 animations:^{
        self.chooseView.frame = CGRectMake(kWidth * 20, kHeight * 114, self.searchLeftBtn.frame.size.width, 0);
        [self.doctorBtn removeFromSuperview];
        [self.line1 removeFromSuperview];
        [self.hospitalBtn removeFromSuperview];
        [self.line2 removeFromSuperview];
        [self.illnessBtn removeFromSuperview];
    }];

}



//键盘search键实现
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //失去第一相应者
    [textField resignFirstResponder];
    
    return YES;
    
}
-(void)backBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
